package com.mytx.search.config;

import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * <p>用户搜索客户端</p>
 *
 * @author: XianGuo
 * @date: 2018年01月31日
 */
@Service("searchClient")
public class EsSearchClient implements SearchClient {

    @Autowired
    private RestHighLevelClient client;



//    @Override
//    public List<JSONObject> search(SearchRequest request) {
//        try {
//            SearchResponse response = client.search(request);
//            if (response.getHits() == null) {
//                return null;
//            }
//            List<JSONObject> list = new ArrayList<>();
//            response.getHits().forEach(item -> list.add(JSON.parseObject(item.getSourceAsString())));
//            return list;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }



    public SearchResponse search(SearchRequest request){
        try {
            return client.search(request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


//    @Override
//    public <T> List<T> search(SearchRequest request, Class<T> tClass) {
//        List<JSONObject> searchResponse = this.search(request);
//        if (searchResponse == null) {
//            return null;
//        }
//        List<T> list = new ArrayList<>(searchResponse.size());
//        searchResponse.forEach(item -> list.add(JSON.parseObject(JSON.toJSONString(item), tClass)));
//        return list;
//    }

    public IndexResponse index(IndexRequest request) {


        try {

            return client.index(request);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public GetResponse get(GetRequest request) {
        try {
            return client.get(request);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }

    public DeleteResponse delete(DeleteRequest request) {
        try {
            return client.delete(request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    public UpdateResponse update(UpdateRequest request) {

        try {
            return client.update(request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public BulkResponse bulk(BulkRequest request) {

        try {
            return client.bulk(request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
